@file:JvmName("JsonHelper")
@file:JvmMultifileClass

package nbcp.base.extend
import nbcp.base.component.JavaPropsObjectMapper
import nbcp.base.component.YamlObjectMapper
import org.slf4j.LoggerFactory

private val logger = LoggerFactory.getLogger("JsonHelper")

fun <T> T.ToJavaProps(): String {
    return JavaPropsObjectMapper.INSTANCE.writeValueAsString(this)
}


fun <T> T.ToYaml(): String {
    return YamlObjectMapper.INSTANCE.writeValueAsString(this)
}


inline fun <reified T> String.FromYamlText(): T {
    return this.FromYamlText(T::class.java)
}

fun <T> String.FromYamlText(type: Class<T>): T {
    try {
        return YamlObjectMapper.INSTANCE.readValue(this, type)
    } catch (e: Throwable) {
        logger.error(
            """
转换Yaml出错:
${this}
"""
        );
        throw e;
    }
}



inline fun <reified T> String.FromJavaProps(): T {
    return this.FromJavaProps(T::class.java)
}

fun <T> String.FromJavaProps(type: Class<T>): T {
    try {
        return JavaPropsObjectMapper.INSTANCE.readValue(this, type)
    } catch (e: Throwable) {
        logger.error(
            """
转换Properties出错:
${this}
"""
        );
        throw e;
    }
}
